package com.homegoods.home.presenters;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.ui.view.IView;
import com.homegoods.home.HomeApiUtils;
import com.homegoods.home.model.HomeClassModel;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.SharePreUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import io.reactivex.functions.Consumer;


/**
 * @author QiQi
 * @ClassName: HomePresenter
 * @date 2018/9/6
 * @description
 */
public class HomePresenter extends BasePresenter<IView> {
    private Random random;


    public HomePresenter(IView mView) {
        super(mView);
    }

    public void getMenuItems() {
        HomeApiUtils.getHomeClass(homeClassModels -> HomePresenter.this.onLoadData(homeClassModels), throwable -> {
            throwable.printStackTrace();
        });
    }

    public void onLoadData(List<HomeClassModel> childlist) {
        if (childlist.size() == 0) {
            return;
        }
        List<Fragment> fragments = new ArrayList<>();
        String[] titles = new String[childlist.size()];
        int j = 0;
        for (int i = 0; i < childlist.size(); i++) {
            HomeClassModel child = childlist.get(i);
            titles[j] = child.getTag();
            j++;
            Fragment fragment;
            if (child.getCmd().equals("FullWinWebView")) {
                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_HOME_WEBVIEW).navigation();
                Bundle bundle = new Bundle();
                bundle.putString("url", child.getData().getWebUrl());
                fragment.setArguments(bundle);
            } else if (child.getCmd().equals("HotDirectSeeding")) {
                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_COMMUNITY_LIVE_FRAGMENT).navigation();
            } else {    // PrdctShow
                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_FRAGMENT_HOMEACTIVITY).navigation();
                Bundle bundle = new Bundle();
                bundle.putSerializable("child", child);
                fragment.setArguments(bundle);
            }
//            if ("社区直播".equals(child.getTag())) {
//
//            } else if ("免费社区".equals(child.getTag())) {
//                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_COMMUNITY_FREEDOM_FRAGMENT).navigation();
//            } else {
//
//            }
            fragments.add(fragment);
        }
        mView.initViewPager(fragments, titles);
    }

    public void startAppGetDeviceConfig() {
        HomeApiUtils.startAppGetDeviceConfig(SharePreUtils::saveAppConfig, throwable -> {
        });
    }

}
